package Euler4;

import java.math.BigInteger;

public class PalindromeProduct {
	public static void main(String[] args) {
		int max = 0;
		
		for(int i = 999; i>99; i--)
		{
			if((i*999)<max)
			{
				break;
			}
			
			int prod = 0;
			for(int j = 999; j>99; j--)
			{
				prod = j*i;
				if(isPalindrome(new BigInteger(Integer.toString(prod))))
				{
					if(prod > max)
					{
						max = prod;
					}
				}
			}
		}
		
		System.out.println(max);
	}
	
	private static boolean isPalindrome(BigInteger number) {
        if(number.equals(reverse(number))){
            return true;
        }
        return false;
    }
 
    private static BigInteger reverse(BigInteger number){
    	BigInteger reverse = new BigInteger("0");
     
        while(!number.equals(new BigInteger("0"))){
          reverse = reverse.multiply(new BigInteger("10")).add(number.mod(new BigInteger("10")));
          number = number.divide(new BigInteger("10"));
        }
             
        return reverse;
    }
}
